# -*- coding: utf-8 -*-
# @Time    : 2024/7/8 9:35
# @Author  : wangwei
# @File    : run.py

import os
import pytest

from common.mail import Mail
from common.result import Result
from common.webkeyword import WebKeyword
from excel_ddt import ddt
from get_path import get_pathdir

if __name__ == '__main__':
    os.system('rd /s /q result')
    os.system('rd /s /q report')

    # 执行并生成allure格式的测试结果

    ddt.run_web_cases()
    os.system('allure generate result -o report --clean')
# 把报告发布出来
#     os.system('allure serve ./result')

    sendmail = Mail()
    # # 处理相对路径为绝对路径
    sendmail.mail_info['filepaths'] = [get_pathdir + '\\lib\\cases\\result-电商项目用例.xlsx']
    sendmail.mail_info['filenames'] = ['WEB电商项目用例.xlsx']
    res = Result()
    result = res.get_res('\\lib/cases\\result-电商项目用例.xlsx')
    print(result)
    runtype = result.get("runtype")
    title = result.get("title")
    starttime = result.get("starttime")
    endtime = result.get("endtime")
    casecount = result.get("casecount")
    passrate = result.get("passrate")
    status=result.get("status")
    with open(get_pathdir + r'\lib\module1.html', mode='r', encoding='utf8') as f:
        text = f.read()
        if status=="FAIL":
            text = text.replace("scolor", "red")
        else:
            text = text.replace("scolor", "green")
        text = text.replace("status", f"{status}")
        text = text.replace("passrate", f"{passrate}")
        text = text.replace("casecount", f"{casecount}")
        text = text.replace("endtime", f"{endtime}")
        text = text.replace("starttime", f"{starttime}")
        text = text.replace("title", f"{title}")
        text = text.replace("runtype", f"{runtype}")
    raw_index = ""
    groups = res.get_groups('\\lib\\cases\\result-电商项目用例.xlsx')[1:]
    print(groups)
    for group in groups:
        index_text = '<tr><td width="100" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">分组信息</td><td width="80" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">用例总数</td><td width="80" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">通过数</td><td width="80" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;color: green;">状态</td></tr>'
        group_info = group[0]
        case_num = group[1]
        pass_num = group[2]
        status = group[3]
        index_text = index_text.replace("分组信息", f"{group_info}")
        index_text = index_text.replace("用例总数", f"{case_num}")
        index_text = index_text.replace("通过数", f"{pass_num}")
        if status=="FAIL":
            index_text = index_text.replace('green', "red")
        index_text = index_text.replace("状态", f"{status}")
        raw_index += index_text
        # print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
        # print(raw_index)
        # print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
    text = text.replace("mailbody", raw_index)

    sendmail.send(text)








